package com.iut.controls;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.iut.model.Parcelle;
import com.iut.projectandroid.R;

public class ListParcelAdapter extends ArrayAdapter<Parcelle>
{
	private int resource;
	
	public ListParcelAdapter(Context context, int resource, List<Parcelle> objects) 
	{
		super(context, resource, objects);
		this.resource = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		ViewHolder holder;
		
		if(convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(resource, null);
			
			holder = new ViewHolder();
			
			holder.nameTextView = (TextView) convertView.findViewById(R.id.name_parcel);
			
			holder.typeTextView = (TextView) convertView.findViewById(R.id.type_parcel);
			
			holder.id = getItem(position).getId();
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		Parcelle parcelle = getItem(position);
		String name = parcelle.getName();
		String type = parcelle.getCulture();
		
		holder.nameTextView.setText(name);
		holder.typeTextView.setText(type);
		holder.id = parcelle.getId();
		
		return convertView;
	}	
}
